<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>解构函数</title>
</head>
<body>
    <h1>解构函数</h1>
    <p>解构函数赋值时，针对赋值运算符的一种扩展
        它针对数组和对象进行操作，代码简单易读
    </p>
    <script>
        //使用箭头函数声明对象stu 属性name，age
        let stu=(name,age)=>{
            return{
                uname:name,
                uage:age
            }
        }
        let s=stu('tom',12)
        console.log(s.uname,s.uage);

        //解构函数获得对象的属性值
        let {uname,uage}=s;
        console.log(`${uname},${uage}`);
        //对数组的解构
        let arr=[1,2,3,4,5,6]
        let[a,b,c,d]=arr
        console.log(a,b,c,d);
        
        //
        let obj={
            a1:{'name':'tom'},
            b1:[12,4,6],
            c1:'hello'
        }
        //解构obj
        //let{a1,b1,c1}=obj;
        console.log(a1.name);
        b1.forEach(a=>console.log(a));
        console.log(c1);

        //剩余参数
        //...变量名
        let {a1,...res}=obj;
        console.log(a1.name);
        //通过剩余参数取得某个参数的值
        res.b1.forEach(a=>console.log(a))
console.log(res.c1);


//解构函数设置默认值
let{num1,num2=789}={num1:123,num2:456}//123,456
console.log(num1,num2);

    </script>
</body>
</html>